<?php

/**
 * 图书站全局公共函数库
 * 
 * 分页等杂项数据
 */

/**
 * 获取分页信息
 * @param int $page_per
 *
 * @return array('offset','limit','cur_page','per_page')
 */
function get_page($page_per = 10) {

	$page = empty($_REQUEST['page']) ? 1 : intval($_REQUEST['page']);
	$start = ($page - 1) * $page_per;

	return array('offset' => $start, 'limit' => $page_per, 'cur_page' => $page, 'per_page' => $page_per);
}

/**
 * 获取分页html代码
 */
function get_page_html($pages, $prev_link_css = 'left', $next_link_css = 'right', $cur_page_css = 'active') {

	$page_str = '';

	if ($pages['page_total'] <= 1) {
		return $page_str;
	}

	if ($pages['prev_link']) {
		$page_str .= '<a class="' . $prev_link_css . '" href="' . $pages['prev_link'] . '"></a>';
	}
	foreach ($pages['page_links'] as $num => $link) {
		if ($num == $pages['cur_page']) {
			$page_str .= '<a class="' . $cur_page_css . '" href="' . $link . '">' . $num . '</a>';
		} else {
			$page_str .= '<a href="' . $link . '">' . $num . '</a>';
		}
	}
	if ($pages['next_link']) {
		$page_str .= '<a class="' . $next_link_css . '" href="' . $pages['next_link'] . '"></a>';
	}

	return $page_str;
}

/**
 * 生成内部访问地址
 */
function creat_url($url, $domain = '', $flag = 0, $is_print = true) {

	parse_str($url, $q);

	if ($domain == '') {
		$res = 'http://' . $_SERVER['HTTP_HOST'] . '/';
	} else {
		$res = $domain . '/';
	}

	if ($flag) {
		$res = '';
	}

	if ($is_print) {
		if ($url) {
			echo $res . 'index.php?' . $url;
		} else {
			echo $res;
		}
	} else {
		if ($url) {
			return $res . 'index.php?' . $url;
		} else {
			return $res;
		}
	}
}


function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

	$ckey_length = 4;

	$key = md5($key ? $key : AUTH_KEY);
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	$rndkey = array();
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return '';
		}
	} else {
		return $keyc.str_replace('=', '', base64_encode($result));
	}
}

function set_cookie($key, $value, $life = 0, $httponly = false) {

	if($value == '' || $life < 0) {
		$value = '';
		$life = -1;
	}

	$life = $life > 0 ? time() + $life : ($life < 0 ? time() - 31536000 : 0);
	
	$secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
	if(PHP_VERSION < '5.2.0') {
		setcookie($key, $value, $life, '/', '', $secure);
	} else {
		setcookie($key, $value, $life, '/', '', $secure, $httponly);
	}
}